<template>
  <VForm
    ref="refForm"
    v-model="isFormValid"
  >
    <VRadioGroup
      v-model="styleType"
      inline
      style="margin-block: 24px 24px;"
    >
      <template #label>
        <div><strong>配置风格</strong></div>
      </template>
      
      <VRadio
        v-for="radio in props.option.styleType.list"
        :key="radio.value"
        :label="radio.title"
        color="primary"
        :value="radio.value"
      />
    </VRadioGroup>

    <VRow
      v-if="styleType != 'default'"
      class="lf-config-row"
    >
      <VCol cols="12">
        <VTextarea
          v-if="styleType == 'properties'"
          v-model="data.properties"
          class="lf-config-item"
          counter
          auto-grow
          clearable
          rows="12"
          maxlength="10000"
          color="success"
          :hint="currStyleTypeTitle"
          :label="currStyleTypeTitle"
        />
        <VTextarea
          v-else-if="styleType == 'yaml'"
          v-model="data.yaml"
          class="lf-config-item"
          counter
          auto-grow
          clearable
          rows="12"
          maxlength="10000"
          color="success"
          :hint="currStyleTypeTitle"
          :label="currStyleTypeTitle"
        />
        <VTextarea
          v-else-if="styleType == 'xml'"
          v-model="data.xml"
          class="lf-config-item"
          counter
          auto-grow
          clearable
          rows="12"
          maxlength="10000"
          color="success"
          :hint="currStyleTypeTitle"
          :label="currStyleTypeTitle"
        />
        <VTextarea
          v-else-if="styleType == 'json'"
          v-model="data.json"
          class="lf-config-item"
          counter
          auto-grow
          clearable
          rows="12"
          maxlength="10000"
          color="success"
          :hint="currStyleTypeTitle"
          :label="currStyleTypeTitle"
        />
      </VCol>
    </VRow>

    <VRow
      v-if="styleType == 'default'"
      class="lf-config-row"
    >
      <VRadioGroup
        v-model="configType"
        inline
        style="padding-inline-start: 12px;"
      >
        <template #label>
          <div><strong>规则类型</strong></div>
        </template>
        <div>
          <VRadio
            v-for="radio in props.option.configType.list"
            :key="radio.value"
            :label="radio.title"
            color="primary"
            :value="radio.value"
          />
        </div>
      </VRadioGroup>
      
      <VRadioGroup
        v-if="configType != 0"
        v-model="sourceType"
        inline
        style="padding-inline-start: 12px;"
      >
        <template #label>
          <div><strong>数据源</strong></div>
        </template>
        <div>
          <VRadio
            v-for="radio in props.option.sourceType.list"
            :key="radio.value"
            :label="radio.title"
            color="primary"
            :value="radio.value"
          />
        </div>
      </VRadioGroup>
      <VCol
        v-if="0 == configType"
        cols="12"
      >
        <VTextField
          v-model="ruleSource"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="规则文件路径: liteflow.rule-source=config/flow.el.xml"
          label="liteflow.rule-source"
        />
      </VCol>
      <VCol
        v-if="1 == configType"
        cols="12"
      >
        <VTextarea
          v-model="ruleSourceExtDataMap"
          class="lf-config-item"
          counter
          maxlength="2500"
          color="success"
          hint="rule-source-ext-data-map"
          label="liteflow.rule-source-ext-data-map"
        />
      </VCol>
      <VCol
        v-if="2 == configType"
        cols="12"
      >
        <VTextarea
          v-model="ruleSourceExtData"
          class="lf-config-item"
          counter
          maxlength="2500"
          color="success"
          hint="rule-source-ext-data"
          auto-grow
          rows="5"
          row-height="15"
          label="liteflow.rule-source-ext-data 格式为JSON字符串"
        />
      </VCol>
      <!-- <VDivider /> -->
      <VCol cols="12">
        <VTextField
          v-model="enable"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="liteflow是否开启,默认为true"
          label="liteflow.enable"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="printBanner"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="liteflow的banner打印是否开启,默认为true"
          label="liteflow.print-banner"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="zkNode"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="zkNode的节点，只有使用zk作为配置源的时候才起作用，默认为/lite-flow/flow"
          label="liteflow.zk-node"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="slotSize"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="上下文的最大数量槽,默认值为1024"
          label="liteflow.slot-size"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="mainExecutorWorks"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="FlowExecutor的execute2Future的线程数,默认为64"
          label="liteflow.main-executor-works"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="mainExecutorClass"
          class="lf-config-item"
          counter
          maxlength="128"
          color="success"
          hint="FlowExecutor的execute2Future的自定义线程池com.yomahub.liteflow.thread.LiteFlowDefaultMainExecutorBuilder"
          label="liteflow.main-executor-class"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="requestIdGeneratorClass"
          class="lf-config-item"
          counter
          maxlength="128"
          color="success"
          hint="自定义请求ID的生成类，LiteFlow提供了默认的生成类com.yomahub.liteflow.flow.id.DefaultRequestIdGenerator"
          label="liteflow.request-id-generator-class"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="threadExecutorClass"
          class="lf-config-item"
          counter
          maxlength="128"
          color="success"
          hint="并行节点的线程池Builder，LiteFlow提供了默认的com.yomahub.liteflow.thread.LiteFlowDefaultWhenExecutorBuilder"
          label="liteflow.thread-executor-class"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="whenMaxWaitTime"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="异步线程最长的等待时间(只用于when)，默认值为15000"
          label="liteflow.when-max-wait-time"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="whenMaxWaitTimeUnit"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="异步线程最长的等待时间单位(只用于when)，默认值为MILLISECONDS，毫秒"
          label="liteflow.when-max-wait-time-unit"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="whenMaxWorkers"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="when节点全局异步线程池最大线程数，默认为16"
          label="liteflow.when-max-workers"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="whenQueueLimit"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="when节点全局异步线程池等待队列数，默认为512"
          label="liteflow.when-queue-limit"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="whenThreadPoolIsolate"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="开启WHEN线程池隔离，默认为false"
          label="liteflow.when-thread-pool-isolate"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="parallelLoopMaxWorkers"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="并行循环子项线程池最大线程数，默认为16"
          label="liteflow.parallelLoop-max-workers"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="parallelLoopQueueLimit"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="并行循环子项线程池等待队列数，默认为512"
          label="liteflow.parallelLoop-queue-limit"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="parallelLoopExecutorClass"
          class="lf-config-item"
          counter
          maxlength="128"
          color="success"
          hint="并行循环子项的线程池Builder，LiteFlow提供了默认的com.yomahub.liteflow.test.customThreadPool.CustomThreadBuilder"
          label="liteflow.parallelLoop-executor-class"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="parseOnStart"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="是否在启动的时候就解析规则，默认为true"
          label="liteflow.parse-on-start"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="retryCount"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="全局重试次数，默认为0"
          label="liteflow.retry-count"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="supportMultipleType"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="是否支持不同类型的加载方式混用，默认为false"
          label="liteflow.support-multiple-type"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="nodeExecutorClass"
          class="lf-config-item"
          counter
          maxlength="128"
          color="success"
          hint="全局默认节点执行器com.yomahub.liteflow.flow.executor.DefaultNodeExecutor"
          label="liteflow.node-executor-class"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="printExecutionLog"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="是否打印执行中过程中的日志，默认为true"
          label="liteflow.print-execution-log"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="enableMonitorFile"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="是否开启本地文件监听，默认为false"
          label="liteflow.enable-monitor-file"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="fastLoad"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="是否开启快速解析模式，默认为false"
          label="liteflow.fast-load"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="enableLog"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="监控是否开启，默认不开启"
          label="liteflow.monitor.enable-log"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="queueLimit"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="监控队列存储大小，默认值为200"
          label="liteflow.monitor.queue-limit"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="delay"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="监控一开始延迟多少执行，默认值为300000毫秒，也就是5分钟"
          label="liteflow.monitor.delay"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="period"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="监控日志打印每过多少时间执行一次，默认值为300000毫秒，也就是5分钟"
          label="liteflow.monitor.period"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="fallbackCmpEnable"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="需要在配置文件开启组件降级功能，默认是关闭的"
          label="liteflow.fallback-cmp-enable"
        />
      </VCol>
    </VRow>
  </VForm>
</template>

<script setup>
import { onMounted, reactive, ref, watch } from 'vue'

const props = defineProps({		
  option: Object,
})

const emits = defineEmits(['refreshPage'])

// 表单
const isFormValid = ref(false)
const refForm = ref()

const styleType = ref('default')

const data = reactive({
  default: {},
  properties: '',
  yaml: '',
  xml: '',
  json: '',
})

onMounted(() => {
  ivyDictSelectByCode()
})

const ivyDictSelectByCode = async () => {
  const res = await $ivyApi.post(API_URLS.ivyDictSelectByCode, {
    code: 'ivy_config',
  })

  if(res.data){
    Object.assign(data, JSON.parse(res.data.value))
  }
}

const saveConfig = async () => {
  const res = await $ivyApi.post(API_URLS.ivyDictSaveByCode, {
    code: 'ivy_config',
    value: JSON.stringify(data),
  })

  emits('refreshPage', res)
}


const configType = ref(0)
const sourceType = ref(0)
const ruleSource = ref('')
const ruleSourceExtDataMap = ref('')
const ruleSourceExtData = ref('')
const enable = ref(true)
const printBanner = ref(true)
const zkNode = ref('')
const slotSize = ref(1024)
const mainExecutorWorks = ref(64)
const mainExecutorClass = ref('com.yomahub.liteflow.thread.LiteFlowDefaultMainExecutorBuilder')
const requestIdGeneratorClass = ref('com.yomahub.liteflow.flow.id.DefaultRequestIdGenerator')
const threadExecutorClass = ref('com.yomahub.liteflow.thread.LiteFlowDefaultWhenExecutorBuilder')
const whenMaxWaitTime = ref(15000)
const whenMaxWaitTimeUnit = ref('MILLISECONDS')
const whenMaxWorkers = ref(16)
const whenQueueLimit = ref(512)
const whenThreadPoolIsolate = ref(false)
const parallelLoopMaxWorkers = ref(16)
const parallelLoopQueueLimit = ref(512)
const parallelLoopExecutorClass = ref('com.yomahub.liteflow.thread.LiteFlowDefaultParallelLoopExecutorBuilder')
const parseOnStart = ref(true)
const retryCount = ref(0)
const supportMultipleType = ref(false)
const nodeExecutorClass = ref('com.yomahub.liteflow.flow.executor.DefaultNodeExecutor')
const printExecutionLog = ref(true)
const enableMonitorFile = ref(false)
const fastLoad = ref(false)
const enableLog = ref(false)
const queueLimit = ref(200)
const delay = ref(300000)
const period = ref(300000)
const fallbackCmpEnable = ref(false)

const currStyleTypeTitle = ref()
const currStyleTypeValue = ref()
const styleTypeList = ref(props.option?.styleType.list)

watch(styleType, val =>{
  const style = styleTypeList.value.filter(item => item.value == val)[0]

  currStyleTypeTitle.value = style.title
  currStyleTypeValue.value = style.value
})


// const saveItem = async () => {
//   refForm.value?.validate().then(({ valid }) => {
//     if(valid) {
//       onSubmit()
//     }
//   })
// }

// const onSubmit = async () => {
//   const res = await $ivyApi.post(getUrl(), {
//     id: id.value,
//     configId: configId.value,
//     configName: configName.value,
//     configType: configType.value,
//     sourceType: sourceType.value,
//     ruleSource: ruleSource.value,
//     ruleSourceExtDataMap: ruleSourceExtDataMap.value,
//     ruleSourceExtData: ruleSourceExtData.value,
//     enable: enable.value,
//     printBanner: printBanner.value,
//     zkNode: zkNode.value,
//     slotSize: slotSize.value,
//     mainExecutorWorks: mainExecutorWorks.value,
//     mainExecutorClass: mainExecutorClass.value,
//     requestIdGeneratorClass: requestIdGeneratorClass.value,
//     threadExecutorClass: threadExecutorClass.value,
//     whenMaxWaitTime: whenMaxWaitTime.value,
//     whenMaxWaitTimeUnit: whenMaxWaitTimeUnit.value,
//     whenMaxWorkers: whenMaxWorkers.value,
//     whenQueueLimit: whenQueueLimit.value,
//     whenThreadPoolIsolate: whenThreadPoolIsolate.value,
//     parallelLoopMaxWorkers: parallelLoopMaxWorkers.value,
//     parallelLoopQueueLimit: parallelLoopQueueLimit.value,
//     parallelLoopExecutorClass: parallelLoopExecutorClass.value,
//     parseOnStart: parseOnStart.value,
//     retryCount: retryCount.value,
//     supportMultipleType: supportMultipleType.value,
//     nodeExecutorClass: nodeExecutorClass.value,
//     printExecutionLog: printExecutionLog.value,
//     enableMonitorFile: enableMonitorFile.value,
//     fastLoad: fastLoad.value,
//     enableLog: enableLog.value,
//     queueLimit: queueLimit.value,
//     delay: delay.value,
//     period: period.value,
//     fallbackCmpEnable: fallbackCmpEnable.value,
//   })

//   emits('refreshPage', res)
// }

defineExpose({
  saveConfig,
})
</script>

<style scoped>
.lf-config-row .v-col {
  padding-block-end: 3px;
}

.lf-config-item {
  padding-block-start: 8px;
}
</style>
